// $codepro.audit.disable methodChainLength
/**
 * License under GNU's GPL 
 * Admin.java 
 */


import java.io.IOException;
import java.util.Scanner;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Class represents an Administrator for the system,
 * 
 * @author Team 9
 * @version 1.0
 * 
 */
public class Admin extends Doctor {

	/**
	 * Gui window
	 */
	private AdminGui window;
	
	/**
	 * type of object
	 */
	private int type = 0;
	
	/**
	 * constant value 2
	 */
	private static final int TWO = 2;

	/**
	 * constant value 3
	 */
	private static final int THREE = 3;

	// Logging code
	/**
	 * Logger variable
	 */
	private static final Logger ADMIDLOGGER = Logger.getLogger("Admin");

	static {
		FileHandler filehand = null;
		try {
			filehand = new FileHandler("Admin.log");
		} catch (SecurityException se) {
			ADMIDLOGGER.log(Level.SEVERE,
					"Security Exception creating the logger", se);
			se.printStackTrace();
		} catch (IOException ioe) {
			ADMIDLOGGER.log(Level.SEVERE, "IO Exception creating the logger",
					ioe);
			ioe.printStackTrace();
		}

		filehand.setFilter(new Filter() {
			public boolean isLoggable(LogRecord record) {
				return true;
			}
		});
		ADMIDLOGGER.addHandler(filehand);
		ADMIDLOGGER.setLevel(Level.ALL);
	}// end logging code

	/**
	 * Overrides the toString() method to print the appropriate instance
	 * variables
	 * 
	 * @return String to be printed
	 */
	public String toString() {
		// adminLogger.finest("ToString method");
		return "Admin []";
	}
	
	/**
	 * Creates a User that is an administrator of the system overall.
	 * @param first First name of the admin
	 * @param last Last name of the admin
	 * @param user The admin's username
	 * @param pass The admin's password
	 */
	public Admin(String first, String last, String user, String pass) {
		lastName = last;
		firstName = first;
		userName = user;
		password = pass;
		suspend = false;
		type = 0;
		ADMIDLOGGER.info("Admin constructed: " + first + " " + last);

	}

	/**
	 * Method to add a new user
	 */
	public void addUser() {
		final Scanner scan = new Scanner(System.in);
		System.out
				.println("Enter type of user, 1 for patient, 2 for nurse, 3 for doctor");
		final String type = scan.nextLine();
		System.out.println("Enter first name");
		final String first = scan.nextLine();
		System.out.println("Enter last name");
		final String last = scan.nextLine();
		System.out.println("Enter user name");
		final String user = scan.nextLine();
		System.out.println("Enter password name");
		final String pass = scan.nextLine();

		try {
			final int typeOfUser = Integer.parseInt(type);

			if (typeOfUser == 1) {
				final Patient patient = new Patient(first, last, user, pass);
				Gui.addToPatientArray(patient);
			} else if (typeOfUser == TWO) {
				final Nurse nurse = new Nurse(first, last, user, pass);
				Gui.addToNurseArray(nurse);
			} else if (typeOfUser == THREE) {
				final Doctor doctor = new Doctor(first, last, user, pass);
				Gui.addToDoctorArray(doctor);
			} else {
				System.out.println("Invalid user type.");
			}
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
		}
		ADMIDLOGGER.info("User added");
	}

	/**
	 * Method to search for an existing nurse
	 * 
	 * @param first
	 *            first name of the nurse
	 * @param last
	 *            last name of the nurse
	 * @return index of found nurse else a negative integer
	 */
	public int nurseLookup(String first, String last) {
		int counter = 0;
		while (counter < Gui.getNurseArray().size()) {
			if ((Gui.getNurseArray().get(counter).getFirstName()
					.compareTo(first) == 0)
					&& (Gui.getNurseArray().get(counter).getLastName()
							.compareTo(last) == 0)) {
				counter++;
				//break;
			}else {
				counter++;
			}
		}
		if (counter == Gui.getNurseArray().size()) {
			return -counter;
		} else {
			return counter;
		}
	}

	/**
	 * Method to search for an existing doctor
	 * 
	 * @param first
	 *            first name of the doctor
	 * @param last
	 *            last name of the doctor
	 * @return index of found doctor else a negative integer
	 */
	public int doctorLookup(String first, String last) {
		int counter = 0;
		while (counter < Gui.getDoctorArray().size()) {
			if ((Gui.getDoctorArray().get(counter).getFirstName()
					.compareTo(first) == 0)
					&& (Gui.getDoctorArray().get(counter).getLastName()
							.compareTo(last) == 0)) {
				counter++;
				//break;
			}else {
				counter++;
			}
		}
		if (counter == Gui.getDoctorArray().size()) {
			return -counter;
		} else {
			return counter;
		}
	}

	/**
	 * Method to edit a user
	 * 
	 * @param first
	 *            first name of the user to edit
	 * @param last
	 *            last name of the user to edit
	 */
	public void editUser(String first, String last) {
		final Scanner scan = new Scanner(System.in);
		System.out
				.println("Enter type of user, 1 for patient, 2 for nurse, 3 for doctor");
		final String type = scan.nextLine();

		try {
			final int typeOfUser = Integer.parseInt(type);

			if (typeOfUser == 1) {
				editPatient(first, last);
			} else if (typeOfUser == TWO) {
				final int index = nurseLookup(first, last);

				if (index < 0) {
					System.out.println("Nurse not found");
				} else {
					System.out.println("Enter first name");
					final String firstName = scan.nextLine();
					System.out.println("Enter last name");
					final String lastName = scan.nextLine();
					System.out.println("Enter user name");
					final String userName = scan.nextLine();
					System.out.println("Enter password name");
					final String pass = scan.nextLine();

					Gui.getNurseArray().get(index).setLastName(lastName);
					Gui.getNurseArray().get(index).setFirstName(firstName);
					Gui.getNurseArray().get(index).setUserName(userName);
					Gui.getNurseArray().get(index).setPassword(pass);
				}
			} else if (typeOfUser == THREE) {
				final int index = doctorLookup(first, last);

				if (index < 0) {
					System.out.println("Patient not found");
				} else {
					System.out.println("Enter first name");
					final String firstName = scan.nextLine();
					System.out.println("Enter last name");
					final String lastName = scan.nextLine();
					System.out.println("Enter user name");
					final String userName = scan.nextLine();
					System.out.println("Enter password name");
					final String pass = scan.nextLine();

					Gui.getDoctorArray().get(index).setLastName(lastName);
					Gui.getDoctorArray().get(index).setFirstName(firstName);
					Gui.getDoctorArray().get(index).setUserName(userName);
					Gui.getDoctorArray().get(index).setPassword(pass);
				}
			} else {
				System.out.println("Invalid entry");
			}
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
		}
		ADMIDLOGGER.info("Admin edited User: " + first + " " + last);
	}

	/**
	 * Method to delete a user
	 * 
	 * @param first
	 *            first name of the user to delete
	 * @param last
	 *            last name of the user to delete
	 */
	public void deleteUser(String first, String last) {
		final Scanner scan = new Scanner(System.in);
		System.out
				.println("Enter type of user, 1 for patient, 2 for nurse, 3 for doctor");
		final String type = scan.nextLine();

		try {

			final int typeOfUser = Integer.parseInt(type);

			if (typeOfUser == 1) {
				delPatient(first, last);
			} else if (typeOfUser == TWO) {
				final int index = nurseLookup(first, last);
				if (index > 0) {
					Gui.getNurseArray().remove(index);
				} else {
					System.out.println("Nurse not found");
				}
			} else if (typeOfUser == THREE) {
				final int index = doctorLookup(first, last);
				if (index > 0) {
					Gui.getNurseArray().remove(index);
				} else {
					System.out.println("Doctor not found");
				}
			} else {
				System.out.println("Invalid entry");
			}
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
		}
		ADMIDLOGGER.info("Admin deleted User: " + first + " " + last);
	}

	/**
	 * Method to reset a user's password
	 * 
	 * @param username
	 *            username of the user
	 */
	public void resetPassword(String username) {
		// final User user = new User();
		final List<User> users = Gui.getUsers();
		final Scanner scan = new Scanner(System.in);
		boolean flag = false;

		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUserName().compareTo(username) == 0) {
				System.out.println("Enter new password");
				String pass = scan.nextLine();
				users.get(i).setPassword(pass);
				flag = true;

			}
		}
		if (!flag) {
			System.out.println("User not found");
		}
		ADMIDLOGGER.config("Admin edited password for User: " + username);
	}

	/**
	 * Method to generate a GUI for the admin
	 */

	public void showGUI() {
		window = new AdminGui();
		window.getFrame().setVisible(true);

	}

	/**
	 * Method to suspend/unsuspend a user
	 * 
	 * @param username
	 *            username of the user
	 * @param suspend
	 *            boolean state, true if suspended otherwise false
	 */
	public void suspend(String username, boolean suspend) {
		//
		final List<User> users = Gui.getUsers();
		boolean flag = false;

		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUserName().compareTo(username) == 0) {
				users.get(i).setSuspend(suspend);
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("User not found");
		}
		ADMIDLOGGER.info("Admin susepended User: " + username);
	}

}
